var register_data = {};
var curWebview;
var paredntWebview;
var chat = {
	//连接建立时发送登录信息
	onopen : function(){
		ws.send(JSON.stringify(register_data));
	},
	onmessage : function(e){
		var data = eval("("+e.data+")");
	    switch(data['type']){
	        case 'ping':
	            ws.send('{"type":"pong"}');
	            break;
	        case 'register':
	            if(data.state != 200){
	            	mui.toast(data.msg);
	            	return false;
	            }
	            localStorage.setItem("username",data.username);
	            localStorage.setItem("nickname",data.nickname);
	            localStorage.setItem("usertype",data.usertype);
	            localStorage.setItem("client_id",data.client_id);
	            localStorage.setItem("token",data.token);
	            mui.back();
	            break;
	    }
	},
};
mui.init();
mui.plusReady(function(){
	curWebview = plus.webview.currentWebview();
	paredntWebview = curWebview.parent();
	//back时关闭窗口
	curWebview.addEventListener('hide', function() {
		loginWebview.close();

	}, false);
	name = localStorage.getItem("username");
	usertype = localStorage.getItem("usertype");
	into_room_id = localStorage.getItem("room_id");
	mui("body").on("tap","button#registerSubmit",function(e){
		var userne = document.getElementById("username");
		var passwd = document.getElementById("password");
		var repasswd = document.getElementById("repassword");
		var nickname = document.getElementById("nickname");
		var usertype = document.getElementById("usertype");
		if(userne.value.trim()===""){
			mui.toast("请输入用户名");
			userne.focus();
			return false;
		}
		if(passwd.value.trim()===""){
			mui.toast("请输入登录密码");
			passwd.focus();
			return false;
		};
		if(passwd.value != repasswd.value){
			mui.toast("两次密码输入不一致");
			repasswd.focus();
			return false;
		}
		if(nickname.value.length == 0){
			mui.toast("请输入玩家昵称");
			nickname.focus();
			return false;
		}
		register_data = {
			'username' : userne.value,
			'nickname' : nickname.value,
			'password' : passwd.value,
			'repassword' : repasswd.value,
			'usertype' : usertype.value,
			'type' : 'register',
		}
		room_connect(chat.onopen,chat.onmessage);
	});
});
